//package com.star.common.monitor.domain.robot.model;
//
//import ch.qos.logback.classic.LoggerContext;
//import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
//import ch.qos.logback.classic.spi.ILoggingEvent;
//import ch.qos.logback.core.Layout;
//import ch.qos.logback.core.UnsynchronizedAppenderBase;
//import ch.qos.logback.core.encoder.Encoder;
//import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
//import com.google.common.util.concurrent.RateLimiter;
//import com.star.common.core.context.SpringContext;
//import com.star.common.monitor.domain.external.send.MonitorSender;
//import com.star.common.monitor.infras.config.BaseMonitorProperties;
//import lombok.Data;
//import lombok.EqualsAndHashCode;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.util.StringUtils;
//
//import java.nio.charset.StandardCharsets;
//import java.util.Collections;
//
//
///**
// * 实现异步 日志队列
// *
// * @author Jensen
// * @公众号 架构师修行录
// */
//
//@Data
//@Slf4j(topic = "### BASE-MONITOR : RobotAppender ###")
//@EqualsAndHashCode(callSuper = true)
//public class RobotAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
//
//    /**
//     * 每个机器人每分钟最多发送20条
//     */
//    private static RateLimiter rateLimiter;
//
//    /**
//     * 发送速率 [每分钟最多20次] 1/3.5~= 0.2857
//     */
//    private Double rateLimiterPermitsPerSecond = 0.2857;
//
//    /**
//     * 定义 layout 处理器 Encode
//     *
//     * @see PatternLayoutEncoder
//     * @see LayoutWrappingEncoder
//     * {@see http://logback.qos.ch/manual/encoders.html}
//     */
//    private Encoder<ILoggingEvent> encoder;
//
//    public RobotAppender() {
//        super();
//        super.setName("dRobot");
//    }
//
//    @Override
//    protected void append(ILoggingEvent eventObject) {
//        if (encoder == null) {
//            addWarn("encoder is null");
//            return;
//        }
//        byte[] encodeBytes = encoder.encode(eventObject);
//        try {
//            acquire();
//            SpringContext.getBean(MonitorSender.class).send(new String(encodeBytes, StandardCharsets.UTF_8), Collections.emptyList(), false);
//        } catch (Exception e) {
//            log.error("send robot error", e);
//        }
//    }
//
//    /**
//     * 设置 layout
//     */
//    public void setLayout(Layout<ILoggingEvent> layout) {
//        LayoutWrappingEncoder<ILoggingEvent> customLayoutEncoder = new LayoutWrappingEncoder<>();
//        customLayoutEncoder.setLayout(layout);
//        customLayoutEncoder.setContext(context);
//        this.encoder = customLayoutEncoder;
//    }
//
//    public static RobotAppender build(LoggerContext loggerContext) {
//        RobotAppender robotAppender = new RobotAppender();
//        robotAppender.setRateLimiterPermitsPerSecond(SpringContext.getBean(BaseMonitorProperties.class).getLog().getRateLimiterPermitsPerSecond());
//        robotAppender.setContext(loggerContext);
//        RobotLayout layout = buildRobotLayout(loggerContext);
//        layout.setMdcList(SpringContext.getBean(BaseMonitorProperties.class).getLog().getConfig().getMdcList());
//        layout.start();
//        robotAppender.setLayout(layout);
//        robotAppender.start();
//        return robotAppender;
//    }
//
//    /**
//     * 构建RobotLayout
//     */
//    private static RobotLayout buildRobotLayout(LoggerContext loggerContext) {
//        RobotLayout layout = new RobotLayout();
//        layout.setContext(loggerContext);
//        BaseMonitorProperties.Log.App applicationConfig = SpringContext.getBean(BaseMonitorProperties.class).getLog().getApp();
//        if (applicationConfig == null) {
//            applicationConfig = new BaseMonitorProperties.Log.App();
//        }
//        String app = applicationConfig.getName();
//        if (!StringUtils.hasText(app)) {
//            app = SpringContext.getEnv().getProperty("spring.application.name");
//        }
//        layout.setApp(app);
//        String project = applicationConfig.getProject();
//        if (StringUtils.hasText(project)) {
//            layout.setProject(project);
//        }
//        return layout;
//    }
//    private static synchronized void acquire() {
//        if (rateLimiter == null) {
//            BaseMonitorProperties properties = SpringContext.getBean(BaseMonitorProperties.class);
//            rateLimiter = RateLimiter.create(properties.getLog().getRateLimiterPermitsPerSecond());
//        }
//        rateLimiter.acquire();
//    }
//}
